package com.chenyongjing.design.pattern.singleton.LazyDoubleSingleton;

/**
 * 优点：性能高，线程安全了
 * 缺点：可读难度加大，不够优雅
 */
public class LazyDoubleCheckSingleton {
    // volatile 防止线程紊乱
    private volatile static LazyDoubleCheckSingleton instance;

    private LazyDoubleCheckSingleton(){}

    public  static LazyDoubleCheckSingleton getInstance(){
        // 检查是否阻塞
        if (instance == null) {
            synchronized (LazyDoubleCheckSingleton.class) {
                // 检测是否需要重新创建实列
                if(instance == null) {
                    instance = new LazyDoubleCheckSingleton();
                    // 指令重排序的问题
                }
            }
        }
        return instance;
    }
}
